package com.lanou.controller;

import com.google.gson.Gson;
import com.lanou.service.InsertService;
import com.lanou.utils.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "ChaneCateServlet",urlPatterns = "/changecate")
public class ChaneCateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    private InsertService insertService = new InsertService();
    //修改类
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Integer cid = Integer.parseInt(String.valueOf(session.getAttribute("cid")));
        String cname = request.getParameter("cname");
        response.setContentType("application/json;charset=utf-8");
        AjaxResult ar = new AjaxResult();
        PrintWriter out = response.getWriter();
        int update = insertService.changeCate(cid, cname);
        if (update==1001){
            ar.setCode(update);
            ar.setMsg("修改失败");
        }else if (update==1002){
            ar.setCode(update);
            ar.setMsg("类名已存在");
        }else if (update==1000){
            ar.setCode(update);
            ar.setMsg("修改成功");
        }
        out.write(new Gson().toJson(ar));
        out.close();
    }
}
